

package legstar.trans.choice;
import java.util.Hashtable;

import com.legstar.coxb.CobolElementVisitor;
import com.legstar.coxb.ICobolBinding;
import com.legstar.coxb.ICobolChoiceBinding;
import com.legstar.coxb.ICobolUnmarshalChoiceStrategy;
import com.legstar.coxb.host.HostException;

import legstar.trans.rdef02.Rdef02Record;

/** 
 * Skeleton implementation of a custom choice selection strategy. Modify this
 * code to select a suitable alternative.
 */
public class ComDetailChoice implements ICobolUnmarshalChoiceStrategy {

    /** {@inheritDoc} */
    public ICobolBinding choose(
        final ICobolChoiceBinding choice,
        final Hashtable < String, Object > variablesMap,
        final CobolElementVisitor visitor) throws HostException {
        
        /* Move this code to unmarshaler */
        for (ICobolBinding alternative : choice.getAlternativesList()) {
            alternative.setObjectValue(null);
            
        }
        /* Get the parent value object which properties might help select the
         * right alternative. */
        Rdef02Record valueObject = (Rdef02Record) choice.getParentValueObject();
        assert (valueObject != null);
        
        int index = valueObject.getRdef02Key().getComSelect();
        switch (index) {
        case 0:
            return choice.getAlternativeByName("ComDetail1");
        case 1:
            return choice.getAlternativeByName("ComDetail2");
        case -1:
            /* An exemple of how to signal an exception.*/
            throw (new HostException("Unable to select an alternative"));
        default:
            /* Null will let the default choice strategy apply. */
            return null;
        }
    }
}


